unit Komp_Unit8;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Grids,
  Vcl.DBGrids;

type
  TFormADD_ARREARS = class(TForm)
    PanelARREARS_BOTTOM: TPanel;
    ButtonOK: TButton;
    ButtonCANCEL: TButton;
    PanelARREARS_CLIENT: TPanel;
    GroupBox1: TGroupBox;
    EditSURNAME: TEdit;
    Label1: TLabel;
    EditCASH: TEdit;
    EditARREARS: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    DBGridADD_ARREARS: TDBGrid;
    Label4: TLabel;
    EditPHONE: TEdit;
    Label5: TLabel;
    EditADDRESS: TEdit;
    procedure FormShow(Sender: TObject);
    procedure EditCASHKeyPress(Sender: TObject; var Key: Char);
    procedure EditCASHChange(Sender: TObject);
    procedure EditSURNAMEChange(Sender: TObject);
    procedure ButtonOKClick(Sender: TObject);
    procedure DBGridADD_ARREARSDblClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure EditCASHEnter(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormADD_ARREARS: TFormADD_ARREARS;

implementation

{$R *.dfm}

uses Komp_Unit4, Komp_Unit2, Komp_Unit1, Unit_Share;

procedure TFormADD_ARREARS.ButtonOKClick(Sender: TObject);
var
  cash: Double;
begin
  if EditCASH.Text = '' then
    EditCASH.Text := '0';

  if TryStrToFloat(EditCASH.Text, cash) then
    EditCASH.Text := FloatToStr(cash);

DataModule1.ADOQueryCODE.Active := False;
DataModule1.ADOQueryCODE.SQL.Clear;
DataModule1.ADOQueryCODE.SQL.Add('SELECT *');
DataModule1.ADOQueryCODE.SQL.Add('FROM arrears');
DataModule1.ADOQueryCODE.SQL.Add('WHERE surname = ' + StrForSQL(EditSURNAME.Text));
DataModule1.ADOQueryCODE.Active := True;

  if DataModule1.ADOQueryCODE.RecordCount = 1 then
  begin
    DataModule1.ADOQueryCODE.Active := False;
    DataModule1.ADOQueryCODE.SQL.Clear;
    DataModule1.ADOQueryCODE.SQL.Add('UPDATE arrears');
    DataModule1.ADOQueryCODE.SQL.Add('SET data = NOW(), ticket = CONCAT(ticket, '';' + DataModule1.ADOQueryTICKET.FieldByName('id_bill').AsString + '''), summa = summa + ' + FloatForSQL(StrToFloat(EditARREARS.Text)) + ', phone = ' + StrForSQL(EditPHONE.Text) + ', address = ' + StrForSQL(EditADDRESS.Text));
    DataModule1.ADOQueryCODE.SQL.Add('WHERE surname = ' + StrForSQL(EditSURNAME.Text));
    DataModule1.ADOQueryCODE.ExecSQL;
  end
  else
  begin
    DataModule1.ADOQueryCODE.Active := False;
    DataModule1.ADOQueryCODE.SQL.Clear;
    DataModule1.ADOQueryCODE.SQL.Add('INSERT INTO arrears');
    DataModule1.ADOQueryCODE.SQL.Add('(user, data, summa, surname, ticket, phone, address)');
    DataModule1.ADOQueryCODE.SQL.Add('VALUES (' + StrForSQL(user) + ', NOW(), ' + FloatForSQL(StrToFloat(EditARREARS.Text)) + ', ' + StrForSQL(EditSURNAME.Text) + ', ''' + DataModule1.ADOQueryTICKET.FieldByName('id_bill').AsString + ''', ' + StrForSQL(EditPHONE.Text) + ', ' + StrForSQL(EditADDRESS.Text) + ')');
    DataModule1.ADOQueryCODE.ExecSQL;
  end;
end;

procedure TFormADD_ARREARS.DBGridADD_ARREARSDblClick(Sender: TObject);
begin
  if (DataModule1.ADOQueryARREARS.Active)
    and (DataModule1.ADOQueryARREARS.RecordCount > 0)
  then
  begin
    EditSURNAME.Text := DataModule1.ADOQueryARREARS.FieldByName('surname').AsString;
    EditPHONE.Text := DataModule1.ADOQueryARREARS.FieldByName('phone').AsString;
    EditADDRESS.Text := DataModule1.ADOQueryARREARS.FieldByName('address').AsString;
  end;
end;

procedure TFormADD_ARREARS.EditCASHChange(Sender: TObject);
var
  cash: Double;
begin
  if EditCASH.Text = '' then
    cash := 0
  else
    TryStrToFloat(EditCASH.Text, cash);

  if cash > StrToFloat(Form1.PanelTICKET_SUMM.Caption) then
    EditARREARS.Font.Color := clRed
  else
    EditARREARS.Font.Color := clBlue;

EditARREARS.Text := FormatFloat('0.00', StrToFloat(Form1.PanelTICKET_SUMM.Caption) - cash);

EditSURNAMEChange(nil);
end;

procedure TFormADD_ARREARS.EditCASHEnter(Sender: TObject);
begin
RUS();
end;

procedure TFormADD_ARREARS.EditCASHKeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
     #44, #48..#57, #8: Key := Key;
  else
    Key := #0;
  end;
end;

procedure TFormADD_ARREARS.EditSURNAMEChange(Sender: TObject);
begin
DataModule1.ADOQueryARREARS.Active := False;
DataModule1.ADOQueryARREARS.SQL.Clear;
DataModule1.ADOQueryARREARS.SQL.Add('SELECT *');
DataModule1.ADOQueryARREARS.SQL.Add('FROM arrears');
DataModule1.ADOQueryARREARS.SQL.Add('WHERE surname LIKE (''%' + EditSURNAME.Text + '%'')');
DataModule1.ADOQueryARREARS.Active := True;

  if (EditSURNAME.Text <> '')
    and (StrToFloat(EditARREARS.Text) > 0)
  then
    ButtonOK.Enabled := True
  else
    ButtonOK.Enabled := False;
end;

procedure TFormADD_ARREARS.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
DBGridToIni(FormADD_ARREARS);
end;

procedure TFormADD_ARREARS.FormShow(Sender: TObject);
begin
DBGridFromIni(FormADD_ARREARS);

EditSURNAME.Text := '';
EditPHONE.Text := '';
EditADDRESS.Text := '';
EditARREARS.Text := '';
EditCASH.Text := '';

EditCASH.SetFocus;
EditSURNAMEChange(nil);
EditCASHChange(nil);
end;

end.
